In this Getting Started, we will use Visual Studio (on Windows) create a console application running that will subscribe to datasets produced by an OPC UA demo publisher, and display the received data on the console. You can target .NET Framework, or .NET 6 or 7.
Note: The same code will work under Linux or macOS as well, but the steps to create the program are different - you will need to use .NET CLI Tools, or other IDE such as JetBrains Rider.
Start Visual Studio.
In this step, we will create a new .NET console application project.
Select Console App or Console App (.NET Framework), in C#. If given a choice of framework, make sure you select one of the supported .NET Runtimes. Finalize the dialog, creating the project.
-> -> from the menu. The dialog(s) that follow differ in different versions of Visual Studio, so we wil just describe the intent here: SelectIf you were not given a choice of framework, right-click on the project node in the Solution Explorer window, select , and check the Target Framework setting.
In this step, we will add a reference to the OpcLabs.QuickOpc NuGet package.
Switch to the Solution Explorer window, right-click on the project node (ConsoleAppn), and select command. In the NuGet: project window, switch to the Browse tab, and type OpcLabs.QuickOpc into the search box. Select the OpcLabs.QuickOpc package in the package list in the left pane of the window. In the right pane of the window, verify or change the package version next to the Version label. The version should be "Latest stable 5.72.build" (where build is a build number). Press the button.
Open the Program.cs file, and add following code to the beginning of the file:
using OpcLabs.EasyOpc.UA.PubSub;
In Program.cs, replace the body of the Main method by following code (use Copy&Paste from here):
var subscriber = new EasyUASubscriber(); subscriber.SubscribeDataSet("opc.udp://239.0.0.1", (_, eventArgs) => { if (eventArgs.Succeeded) { if (!(eventArgs.DataSetData is null)) { Console.WriteLine(); Console.WriteLine($"Dataset data: {eventArgs.DataSetData}"); foreach (var pair in eventArgs.DataSetData.FieldDataDictionary) Console.WriteLine(pair); } } else { Console.WriteLine(); Console.WriteLine($"*** Failure: {eventArgs.ErrorMessageBrief}"); } }); Console.ReadLine(); subscriber.UnsubscribeAllDataSets();
Visit Tool Downloads page and download and unpack OPC UA Demo Publisher (the easiest to work with is Self-extracting EXE, but other format will do as well). In accordance with instructions on the UADemoPublisher Basics page, run the OPC UA Demo Publisher without further parameters.
The OPC UA Demo Publisher will start broadcasting messages that our application will receive. You will see a progress indication (message counts) in its console window. You can run the OPC UA Demo Publisher either on the same computer where you are developing the application, or on a different computer on the same local network (IP subnet).
Select F5) from the menu, or press the corresponding button on the toolbar.
-> (This will build and launch the program. The datasets from the publisher will be received by your subscriber application, and displayed on the console. Press Enter to exit the program.
See OPC UA PubSub Common Traps And Pitfalls if things do not work as expected - and specifically, if your subscriber application is not receiving anything and is giving you timeout errors.
Probably the most common issue is the necessity to properly specify a (non-default) network interface. For the OPC UA Demo Publisher, this is done using the --ConnectionNetworkInterface (-cni) option. For a suggestion about how this is done in your application, see e.g. the code comments in Examples - OPC UA PubSub - Callback using a lambda.